#!usr/bin/env python3
# -*- coding: utf-8 -*-

'main calculation process'

_author_='Nan WANG'

import equipment as ec
import openpyxl as xl
import os

'''主要输入参数'''
inlet_flow = int(input(r'输入设计水量(m3/d):'))
flux_try = int(input(r'输入需要的设计通量（10-25LMH):'))
rate_mem2oxtank = int(input(r'输入膜池至好氧池回流比（2-5倍):'))

inlet_flow_h = inlet_flow / 24              # 单位m3/h


time_fil = float(input(r'输入产水时间(7-10min):'))
time_stop = float(input(r'输入停止时间(1-2min):'))
time_cycle = time_fil + time_stop
time_permeate_percent = time_fil / time_cycle

'''清洗参数'''
MC_cycle = 3               #MC清洗频率
MC_time = 30                #MC清洗时间
MC_NaClO = 200              #NaClO浓度
RC_cycle = 180
RC_NaClO = 1000
RC_NaOH = 500
RC_CA = 0.005               #柠檬酸浓度

# --------------------------设计与计算---------------------------

'''膜组器设计'''
# 选择膜元件
mod = ec.Module(30, 60, flux_try)
num_module = int(inlet_flow_h * 1000 / mod.area_m / mod.flux // 1 + 1)
# 确定膜池数量及单池膜组器数量
if num_module >=32:
    num_skid = 8
    num_tank = num_module // num_skid
elif num_module >= 12:
    num_skid = 6
    num_tank = num_module // num_skid
elif num_module >= 10:
    num_skid = 5
    num_tank = 2
else:
    num_skid = num_module
    num_tank = 1

# 总膜面积
area_total = num_module * mod.area_m
# 设计膜通量
flux_design = inlet_flow_h * 1000 / area_total

'''水泵设计'''
# 1 产水泵
flow_per = 1.1 * inlet_flow_h / time_permeate_percent / num_tank
pump_per = ec.Pump(flow_per, 10, num_tank, 1, '产水泵', '变频')

# 2 混合液回流泵
flow_re = inlet_flow_h * rate_mem2oxtank
pump_re = ec.Pump(flow_re, 1, 4, 1, '膜池混合液回流泵', '')

# 3 反洗泵
flow_backwash = 20 / 1000 * mod.area_m * num_skid  # input(r'反洗通量(LMH)')
pump_backwash = ec.Pump(flow_backwash, 10, 1, 1, '反洗泵', 'SS304', '变频' )

# 4 闸门与调节堰
flow_memtank = inlet_flow_h * ( rate_mem2oxtank +1 ) / num_tank
gate_in = ec.Gate_square(num_tank, flow_memtank)
gate_aj = ec.Gate_rect(num_skid, flow_memtank, 800, 800, 0.7, '手电动可调节堰')

# 5 阀门
# 5.1泵后出水阀
valve_pumpout = ec.Valve(num_tank, flow_per, -1)
valve_pumpout.dia = valve_pumpout.dia_water_post()

# 手动阀门

# 5.2 出水总管阀
valve_totalper = ec.Valve(1, inlet_flow_h, 0, 1.0, '手动阀门')
valve_totalper.dia = valve_totalper.dia_water_normal()

# 5.3 单泵进水管道阀
valve_pumpin= ec.Valve(num_tank, flow_per, 1, 1.0, '手动阀门')
valve_pumpin.dia = valve_pumpin.dia_water_pre()

# 5.4 单泵出水管道阀
valve_hand_pumpout = ec.Valve(num_tank, flow_per, -1, 1.0, '手动阀门')
valve_hand_pumpout.dia = valve_pumpin.dia_water_post()

'''曝气系统设计'''
intensity_aeration = float(input(r'输入膜片曝气强度(1.5-2.5Nm3/h/膜片):'))


# 5.5 单池曝气管道
# valve_gas2tank = ec.Valve(num_tank,)

















#----------------------------打印与输出-----------------------------------
os.chdir(os.path.dirname(__file__))
path_excel = os.getcwd() + r'\BOQ.xlsx'
wb = xl.Workbook(path_excel)

# font_excel = xl.styles.Front(name=u'微软雅黑', bold=False, istalic=False, size=12) 

wb.save(path_excel)

wb = xl.load_workbook(path_excel)
ws = wb['Sheet']

# 分表头1
ws.append(['膜池产水、补水及混合液回流系统'])
#1MBR膜组件
row1 = [
mod.name, 
'设计通量' + str(round(flux_design,2)) +'LMH', 
'台', 
num_module
]
ws.append(row1)

#2产水泵
row2 = [
pump_per.name,
'Q='+str(round(flow_per, 2))+'m3/h, H='+str(pump_per.head)+'m, N='+str(pump_per.Power_motor())+'kW',
'台',
pump_per.qlt + pump_per.qlt_back,
str(pump_per.qlt)+'用'+str(pump_per.qlt_back)+'冷备，变频'
]
ws.append(row2)

#3混合液回流泵
row3 = [
pump_re.name,
'Q='+str(round(flow_re, 2))+'m3/h, H='+str(pump_re.head)+'m, N='+str(pump_re.Power_motor())+'kW',
'台',
pump_re.qlt + pump_re.qlt_back,
str(pump_per.qlt)+'用'+str(pump_per.qlt_back)+'冷备，变频'
]
ws.append(row3)

#4手电动进水方闸门
row4 = [
gate_in.name,
'B×H='+str(gate_in.height)+'mm×'+str(gate_in.height)+'mm',
'台',
gate_in.qlt,
'膜池进阀门,含所有安装附件'
]
ws.append(row4)

#5手电动可调节堰
row5 = [
gate_aj.name,
'B×H='+str(gate_aj.length)+'mm×'+str(gate_aj.height)+'mm',
'台',
gate_aj.qlt,
'膜池出水闸门,双向止水,含所有安装附件'
]
ws.append(row5)

#6单泵出水管道
row6 = [
valve_pumpout.name,
'DN'+str(valve_pumpout.dia)+',P='+str(valve_pumpout.pressure)+'MPa',
'个',
valve_pumpout.qlt,
'单泵出水管道'
]
ws.append(row6)

#7出水总管
row7 = [
valve_totalper.name,
'DN'+str(valve_totalper.dia)+',P='+str(valve_totalper.pressure)+'MPa',
'个',
valve_totalper.qlt,
'出水总管'
]
ws.append(row7)

#8单泵进水管道
row8 = [
valve_pumpin.name,
'DN'+str(valve_pumpin.dia)+',P='+str(valve_pumpin.pressure)+'MPa',
'个',
valve_pumpin.qlt,
'单泵进水管道'
]
ws.append(row8)

#9单泵出水管道
row9 = [
valve_hand_pumpout.name,
'DN'+str(valve_hand_pumpout.dia)+',P='+str(valve_hand_pumpout.pressure)+'MPa',
'个',
valve_hand_pumpout.qlt,
'单泵出水管道'
]
ws.append(row9)

#10碟式止回阀
row10 = [
'碟式止回阀',
'DN'+str(valve_pumpout.dia)+',P='+str(valve_pumpout.pressure)+'MPa',
'个',
valve_pumpout.qlt,
'单泵出水管道,立式安装'
]
ws.append(row10)

#11碟式止回阀
row11 = [
'柔性接头',
'DN'+str(valve_pumpout.dia)+',P='+str(valve_pumpout.pressure)+'MPa',
'个',
valve_pumpout.qlt,
'单泵出水管道,立式安装'
]
ws.append(row11)

#12单泵进水管道
row12 = [
'柔性接头',
'DN'+str(valve_pumpin.dia)+',P='+str(valve_pumpin.pressure)+'MPa',
'个',
valve_pumpin.qlt,
'单泵进水管道'
]
ws.append(row12)

# 分表头2
ws.append(['膜池曝气管道'])
# 13单池曝气管道







# 插入表头
ws.insert_cols(1)
ws.insert_rows(1)
head = [
'编号'
'名称'
'规格型号'
'单位'
'数量'
'备注'
]
ws.append(head)
ws.freeze_panes = 'B2'


wb.save(path_excel)
print('设备清单已保存至当前目录')

os.system("pause") # 暂停console，方便观察输入数值